package cn.echogo.framework.widget.imagezoom;

/**
 * https://gist.github.com/3781644
 * 
 * @version 1.0
 * @author Stan Rong 2012-10-11
 */
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class CopyOfImageViewTouchViewPager extends ViewPager {

	private static final String TAG = "ImageViewTouchViewPager";

	public CopyOfImageViewTouchViewPager(Context context) {
		super(context);
		init();
	}

	public CopyOfImageViewTouchViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	@Override
	protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
		if (v instanceof ImageViewTouch) {
			return ((ImageViewTouch) v).canScroll(dx);
		} else {
			return super.canScroll(v, checkV, dx, x, y);
		}
	}

	private void init() {
		setOnPageChangeListener(new SimpleOnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int state) {
				super.onPageScrollStateChanged(state);
				try {
					ImageViewTouch imageViewTouch = (ImageViewTouch) getChildAt(getCurrentItem());
					if (imageViewTouch != null) {
						imageViewTouch.zoomTo(1f, 300);
					}
				}
				catch (ClassCastException ex) {
					Log.e(TAG, "This view pager should have only ImageViewTouch as a children.", ex);
				}
			}
		});
	}
}
